package com.apes.fn.fico.debitnote.model;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.finance.model.BankAccount;
import com.apes.scm.masterdata.finance.model.PaymentMethodType;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;

/**
 * @BelongsProject: apes-server
 * @BelongsPackage: com.apes.fn.fico.debitnote.model
 * @ClassName: LoanNoteItem
 * @Author: pyb
 * @Description: 放款明细
 * @CreateTime: 2021-01-13 17:33
 * @Version: 1.0
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = LoanNoteItem.CONST_TABLE_NAME)
public class LoanNoteItem extends BaseModel {
    public static final String CONST_TABLE_NAME = "fn_loan_note_item";
    @Id
    @Column(name = "id", length = 30)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "借支放款单明细", padding = 10)
    @Comment("编码")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="loan_note", referencedColumnName="id", nullable = false)
    @Comment("借支放款单")
    private LoanNote loanNote;

    @Column(name = "origin", length = 30)
    @Comment("源单号")
    private String origin;

    @Column(name = "origin_business_type", length = 32)
    @Comment("源单类型")
    private String originBusinessType;


    @Column(name = "amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("付款金额")
    private double amount = 0;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="payment_method_type", referencedColumnName="id", nullable = false)
    @Comment("付款方式")
    private PaymentMethodType paymentMethodType;

    @Comment("银行账户")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "bankAccount_id", referencedColumnName = "id")
    private BankAccount bankAccount;

    @Column(name = "note", length = 250)
    @Comment("摘要")
    private String note;

    @Transient
    @Comment("银行帐号")
    private String bankAccountId;

    public String getBankAccountId(){
        String code = null;
        if (bankAccount != null) {
            code = bankAccount.getAccountId();
        }
        return code;
    }

    @Transient
    private String originBusinessTypeName;

    public String getOriginBusinessTypeName() {
        return getBusinessTypeName("originBusinessType");
    }

}
